<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true">
            <el-form-item label="区块编码" prop="unitCode">
                <el-input v-model="queryParams.unitCode" placeholder="请输入区块编码" clearable style="width: 240px"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="区块名称" prop="unitName">
                <el-input v-model="queryParams.unitName" placeholder="请输入区块名称" clearable style="width: 240px"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <!-- <el-form-item label="创建时间" style="width: 308px">
          <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
            start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
        </el-form-item> -->
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['system:role:add']">新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['system:role:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['system:role:remove']">删除</el-button>
            </el-col>
            <!-- <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['system:role:export']"
            >导出</el-button
          >
        </el-col> -->
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <!-- 表格数据 -->
        <el-table v-loading="loading" :data="tableItems" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="区块编码" prop="unitCode" width="120" />
            <el-table-column label="区块名称" prop="unitName" :show-overflow-tooltip="true" width="150" />
            <el-table-column label="区块地址" prop="location" :show-overflow-tooltip="true" width="250" />
            <el-table-column label="备注" prop="remark" width="100" />
            <el-table-column label="创建时间" align="center" prop="createdTime">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.createdTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-tooltip content="修改" placement="top">
                        <el-button type="text" icon="Edit" @click="handleUpdate(scope.row)"
                            v-hasPermi="['system:role:edit']">
                        </el-button>
                    </el-tooltip>
                    <el-tooltip content="删除" placement="top">
                        <el-button type="text" icon="Delete" @click="handleDelete(scope.row)"
                            v-hasPermi="['system:role:remove']">
                        </el-button>
                    </el-tooltip>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />
        <!-- 添加或修改配置对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
                <el-form-item label="区块编码" prop="unitId">
                    <el-input v-model="form.unitCode" placeholder="请输入单元编码" />
                </el-form-item>
                <el-form-item label="区块名称" prop="unitName">
                    <el-input v-model="form.unitName" placeholder="请输入单元名称" />
                </el-form-item>
                <el-form-item label="区块地址" prop="location">
                    <el-input v-model="form.location" placeholder="请输入单元地址" />
                </el-form-item>
                <el-form-item label="备注">
                    <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>

    </div>
</template>
<script setup name="Unit">
    // curd 顺序导入
import { addUnit as addItem, deleteUnit as deleteItem, updateUnit as updateItem, listUnit as itemList, getUnit as getItem } from "@/api/core/index";

const { proxy } = getCurrentInstance();
const itemName = ref('区块');
const ids = ref([]);
const open = ref(false);
const single = ref(true);
const multiple = ref(true);
const loading = ref(false);
const dateRange = ref([]);
const showSearch = ref(true);
const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        order:'AES'
    }
});

const { queryParams, form } = toRefs(data);
const total = ref(0);
const tableItems = ref([]);
/** 重置新增的表单以及其他数据  */
function reset() {
    proxy.resetForm("formRef");
}
/** 取消按钮 */
function cancel() {
    open.value = false;
    reset();
}
function getList() {
    loading.value = true;
    itemList(queryParams.value).then(
        (res) => {
            console.log(res);
            loading.value = false;
            tableItems.value = res.content;
            total.value = res.totalElements;
        }
    );
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}
/** 重置按钮操作 */
function resetQuery() {
    dateRange.value = [];
    proxy.resetForm("queryRef");
    handleQuery();
}

/** 添加角色 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加" + itemName.value;
}
/** 修改角色 */
function handleUpdate(row) {
    reset();
    const id = row.id || ids.value;
    getItem(id).then((response) => {
        form.value = response;
        open.value = true;
        title.value = "修改" + itemName.value;
    });
}
/** 删除按钮操作 */
function handleDelete(row) {
    const delIds = row.id || ids.value;
    proxy.$modal
        .confirm('是否确认删除编号为"' + delIds + '"的数据项?')
        .then(function () {
            return deleteItem(delIds);
        })
        .then(() => {
            getList();
            proxy.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
    ids.value = selection.map((item) => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}
/** 提交按钮 */
function submitForm() {
    if (form.value.id != undefined) {
        updateItem(form.value).then((response) => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
        });
    } else {
        addItem(form.value).then((response) => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
        });
    }
}
getList();
</script>